package com.bzoa.api.config;

import com.springboot.common.entity.ResponseResult;
import com.springboot.common.entity.ResponseStatus;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 创建者: lands
 * 创建时间:2023/3/3 0:56
 *  全局异常处理器
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理 Exception 异常
     *
     * @param httpServletRequest httpServletRequest
     * @param e                  异常
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseResult exceptionHandler(HttpServletRequest httpServletRequest, Exception e) {
        log.error("服务错误:", e);
        e.printStackTrace();
        return ResponseResult.error(ResponseStatus.SYSTEM_ERROR,e.getMessage());
    }

    /**
     * 处理 BusinessException 异常
     *
     * @param httpServletRequest httpServletRequest
     * @param e                  异常
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public ResponseResult businessExceptionHandler(HttpServletRequest httpServletRequest, BusinessException e) {
        log.info("业务异常code:" + e.getCode() + "msg:" + e.getMessage());
        return ResponseResult.error(ResponseStatus.BUSINESS_ERROR);
    }
}